package com.terry.gamesupport.web;

import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.terry.common.utils.R;
import com.terry.gamesupport.sms.entity.AttachmentEntity;
import com.terry.gamesupport.sms.service.AttachmentService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("sms/upload")
public class FileUploadController {

    @Value("${upload.bashpath}")
    private String uploadBasePath;
    @Value("${upload.wbashpath}")
    private String uploadWBasePath;

    @Autowired
    AttachmentService attachmentService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("/file")
    public R uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("uploadType") int uploadType) throws IOException {
        if(uploadType==1){
            return this.uploadFileCommon(file,"imgs");
        }else{
            return this.uploadFileCommon(file,"roms");
        }
    }

    public R uploadFileCommon(MultipartFile file,String folder){
        if ((file != null && !file.isEmpty())) {
            String filename = file.getOriginalFilename();
            String suffixName = filename.substring(filename.lastIndexOf("."));
            AttachmentEntity attachmentEntity = new AttachmentEntity();
            attachmentEntity.setName(file.getName());
            attachmentEntity.setSize(file.getSize());
            attachmentEntity.setType(suffixName);
            String savePath = uploadBasePath + File.separator + folder + File.separator ;
            String os = System.getProperty("os.name");
            if (os != null && os.toLowerCase().startsWith("windows")) {
                savePath = uploadWBasePath + File.separator + folder + File.separator ;
            }
            try {
                String url = UUID.randomUUID() + suffixName ;
                String desPath = savePath + url;
                File destDir = new File(savePath);
                if (!destDir.exists()) {
                    destDir.mkdirs();
                }
                attachmentEntity.setUrl(url);
                System.out.println(desPath);
                File dest = new File(desPath);

                if (!dest.exists()) {
                    dest.createNewFile();
                }
                file.transferTo(dest);
                attachmentService.save(attachmentEntity);
                rabbitTemplate.convertAndSend("order_event_exchange", "order.create.order", attachmentEntity);
                return R.ok().put("url","/"+folder+"/"+url);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error(ExceptionUtil.getStackTrace(e));
            }
        }
        return R.error();
    }


}
